package com.zito.health.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: gxt
 * @time: 2019/10/12 11:26
 */
@Slf4j
@Controller
@RequestMapping("/fileUpload")
public class UploadController {

    @Value("${imagePath}")
    private String imagePath ;

    @Value("${imageWebUrl}")
    private String imageWebUrl ;

    @ResponseBody
    @RequestMapping("/uploadImage")
    public Map<String, Object> uploadImage(HttpServletRequest request, HttpServletResponse response, @RequestParam(value="imgFile") MultipartFile file){
        Map<String, Object> map = new HashMap<>(16);
        //真实的文件名
        String orignalFileName = file.getOriginalFilename().replaceAll("&","");//去&，有&反显会反显不出来
        // 替用戶去除，符号
        orignalFileName = orignalFileName.replaceAll(",","");
        //生成文件的唯一路径
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        String dateStr = new SimpleDateFormat("yyyyMMddHHmmss").format(c.getTime());
        String tempPath = "upload/"+year + "/" + month + "/" + dateStr+"_" + orignalFileName;
//        String imagePath = "D:/file/";
        //绝对路径
        log.info("根目录："+imagePath);
        String absolutePath = imagePath + tempPath;
        //存储文件
        File destFile = new File(absolutePath);
        File parentFile = destFile.getParentFile();
        if(!parentFile.exists()){
            parentFile.mkdirs();
        }
        InputStream is = null;
        OutputStream os = null;
        try {
            os = new BufferedOutputStream(new FileOutputStream(destFile));
            is = new BufferedInputStream(file.getInputStream());
            int i;
            while((i = is.read()) != -1) {
                os.write(i);
            }
        } catch (FileNotFoundException e1) {
            map.put("success", false);
            map.put("errno", 500);
            map.put("msg", "未成功创建磁盘文件!");
            return map;
        } catch (IOException e2) {
            map.put("success", false);
            map.put("errno", 500);
            map.put("msg", "文件写入错误!");
            return map;
        } finally {
            try {
                is.close();
                os.close();
            } catch (IOException e) {}
        }
        map.put("success", true);
        map.put("errno", 0);
        map.put("msg", "文件写入成功");
		/*List<String> list = new ArrayList<String>();*/
//        String staticWeb = "http://127.0.0.1:82/";
		/*list.add(staticWeb + tempPath);*/
//        String imageWebUrl="D:/file/";
        map.put("filePath", imageWebUrl + tempPath);
        return map;
    }
}
